package x.ovo.wechat.bot.http.request.core;

import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;
import x.ovo.wechat.bot.http.response.QRCodeResponse;

import java.util.Map;
import java.util.function.Function;

public class CheckLoginRequest extends ApiRequest<QRCodeResponse> {

    private final int tip;
    private final String uuid;

    public CheckLoginRequest(LoginSession session, int tip, String uuid) {
        super(session);
        this.tip = tip;
        this.uuid = uuid;
        this.addHeader("Host", "login.wx.qq.com")
                .addHeader("Referer", "https://wx.qq.com/");
    }

    @Override
    public String getUrl() {
        return Constant.LOGIN;
    }

    @Override
    public Map<String, Object> getParameters() {
        super.addParameter("loginicon", true)
                .addParameter("tip", this.tip)
                .addParameter("uuid", this.uuid)
                .addParameter("r", -System.currentTimeMillis() / 1000 / 1579)
                .addParameter("_", System.currentTimeMillis());
        return super.getParameters();
    }

    @Override
    public Function<String, QRCodeResponse> responseHandler() {
        return QRCodeResponse::from;
    }
}
